package cn.aitrox.ry.service.config.dao.impl;

import cn.aitrox.ry.common.dao.MapperSqlCallback;
import cn.aitrox.ry.common.dao.impl.BaseDaoImpl;
import cn.aitrox.ry.service.config.dao.LesionDao;
import cn.aitrox.ry.service.config.entity.LesionEntity;
import cn.aitrox.ry.service.config.mapper.LesionMapper;
import cn.aitrox.ry.util.ArrayUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class LesionDaoImpl extends BaseDaoImpl<LesionEntity> implements LesionDao {

    @Autowired
    private LesionMapper mapper;

    @Override
    public PageInfo<LesionEntity> search(Integer page, Integer pageSize, String likeName, List<String> valueList, Integer parentId) {
        return searchByCustom(page, pageSize, new MapperSqlCallback<LesionEntity>() {
            @Override
            public List<LesionEntity> invoke() {
                return mapper.findByCond(likeName, valueList, parentId);
            }
        });
    }

    @Override
    public List<LesionEntity> findByParenIds(List<Integer> parentIds) {
        if (ArrayUtil.isEmpty(parentIds)) {
            return new ArrayList<>();
        }
        return mapper.findByParenIds(parentIds);
    }

    @Override
    public void batchInsert(List<LesionEntity> list) {
        batchInsert(list, 1000, new MapperSqlCallback<LesionEntity>() {
            @Override
            public void batchInsert(List<LesionEntity> objList) {
                mapper.batchInsert(objList);
            }
        });
    }

    @Override
    public List<LesionEntity> findByValues(List<String> valueList) {
        if (ArrayUtil.isEmpty(valueList)) {
            return new ArrayList<>();
        }
        return mapper.findByValues(valueList);
    }

    @Override
    public void deleteByParentId(Integer parentId) {
        mapper.deleteByParentId(parentId);
    }

    @Override
    public LesionEntity findByParentIdName(Integer parentId, String name) {
        if (null == parentId || null == name) {
            return null;
        }
        LesionEntity cond = new LesionEntity();
        cond.setParentId(parentId);
        cond.setName(name);
        return super.findOne(cond);
    }

    @Override
    public List<LesionEntity> findByNames(List<String> nameList) {
        if (ArrayUtil.isEmpty(nameList)) {
            return new ArrayList<>();
        }
        return mapper.findByNames(nameList);
    }
}
